/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ResultEditor.UnderlinePainting;

import ResultEditor.Annotations.Annotation;
import java.awt.Color;
import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;

/**
 * 1.Bind custom highlightpainter with a given text component.<p>
 * 
 * @author Jianwei Leng
 * @since 06-08-2010 11:40am tuesday
 */
public class HighlightPainter {
    
    protected JTextComponent comp;
    protected Highlighter.HighlightPainter underline_painter;
    //protected Highlighter.HighlightPainter conflict_painter;
    protected Highlighter.HighlightPainter wave_painter;
    


    /**initial method of class <code>HighlightPainter</code>.*/
    public HighlightPainter(JTextComponent comp){
        this.comp = comp;
        this.underline_painter = new UnderlineHighlighter.UnderlineHighlightPainter(Color.black);

        this.wave_painter = new UnderlineHighlighter.WaveHighlightPainter();
        
        // color 'red' only used to indicate which word is current active
        // whatever underline or a color block
    }


    /**
     * set new underline highlight by annotation constructure.
     */
    public void setNewUnderlineHightlight(Annotation annotation){
        try {            
            // get all hightlighter from current text component
            Highlighter highlighter = comp.getHighlighter();

            // Remove any existing underline highlights for last word
            Highlighter.Highlight[] highlights = highlighter.getHighlights();
            for (int i = 0; i < highlights.length; i++) {
                Highlighter.Highlight h = highlights[i];
                 if (h.getPainter() instanceof UnderlineHighlighter.UnderlineHighlightPainter) {
                //8888if (h.getPainter() instanceof UnderlineHighlighter.WaveHighlightPainter) {
                    highlighter.removeHighlight(h);
              }
            }

             highlighter.addHighlight(annotation.spanstart, annotation.spanend, underline_painter);
            //8888highlighter.addHighlight(annotation.spanstart, annotation.spanend, wave_painter);
           
        } catch (Exception ex) {
        
        }
    }

    /**
     * Draw underline highlight by positions
     */
    public void addNewUnderlineHightlight(int _startpoint, int _endpoint){

        // get all hightlighter from current text component
        Highlighter highlighter = comp.getHighlighter();

        try {
            highlighter.addHighlight(_startpoint, _endpoint, underline_painter);
            //8888highlighter.addHighlight(_startpoint, _endpoint, wave_painter);
        } catch (BadLocationException e) {
        // Nothing to do
        }
    }

    /**
     * add Draw underline highlight by positions
     */
    public void addNewWavelineHightlight(int _startpoint, int _endpoint){

        // get all hightlighter from current text component
        Highlighter highlighter = comp.getHighlighter();

        try {
            highlighter.addHighlight(_startpoint, _endpoint, wave_painter);
        } catch (BadLocationException e) {
        // Nothing to do
        }
    }

    public void RemoveAllWaveHighlight(){
        // get all hightlighter from current text component
        Highlighter highlighter = comp.getHighlighter();

        // Remove any existing highlights for last word
        Highlighter.Highlight[] highlights = highlighter.getHighlights();
        for (int i = 0; i < highlights.length; i++) {
            Highlighter.Highlight h = highlights[i];
            if (h.getPainter() instanceof UnderlineHighlighter.WaveHighlightPainter) {
                highlighter.removeHighlight(h);
          }
        }
    }


    /**
     * Call this to clear all underline highlight from Screen of document viewer,
     * including colorful straight underline and red wave underline.
     */
    public void RemoveAllUnderlineHighlight(){
        try{
        // get all hightlighter from current text component
        Highlighter highlighter = comp.getHighlighter();

        // Remove any existing highlights for last word
        Highlighter.Highlight[] highlights = highlighter.getHighlights();
        for (int i = 0; i < highlights.length; i++) {
            Highlighter.Highlight h = highlights[i];
            if (h.getPainter() instanceof UnderlineHighlighter.UnderlineHighlightPainter) {
                highlighter.removeHighlight(h);
            }         
        }
        }catch(Exception ex){
            System.out.println("error 110224");
        }
    }

}
